<?php

namespace App\Admin\Extensions;

use App\Roles;
use Encore\Admin\Form\Field;

class Distpicker extends Field
{

    protected $view = 'admin.distpicker';

    protected static $js = [
        '/js/distpicker.js'
    ];

    public static function getRoles()
    {
        return Roles::all()->pluck('name', 'id');
    }


    public function render()
    {
        $this->addVariables(['roles' => Roles::all()]);
        $this->script = <<<EOF
window.data = {};
window.data.area_id =$("select[name=area_id]").val();
$("#appendsHTMLOnRole1").hide();
$("#appendsHTMLOnRole2").hide();
$("#appendsHTMLOnRole3").hide();
$("select[name=area_id]").change(function () {
     let area_id = $(this).children('option:selected').val();
     
     window.data.area_id = area_id;
});

$("select[name=role_id]").change(function () {
if(!window.data.area_id){
    alert('请先选择区域!');
    return;
 }
$("#appendsForm").text('');
    let role_id = $(this).children('option:selected').val();
    window.data.role_id = role_id;
    $("#appendsForm").append('<input type="hidden" name="role_id" value="' + role_id +'" />')
    if (role_id == 1) {  //大区总经理
        $("#appendsForm").append('<input type="hidden" name="pid" value="0" />')
     
        $("#appendsHTMLOnRole1").hide();
        $("#appendsHTMLOnRole2").hide();
        $("#appendsHTMLOnRole3").hide();
    } else if (role_id == 2) { //大区经理
       getUsers(window.data.area_id, 2);
       $("#appendsHTMLOnRole1").show();
        $("#appendsHTMLOnRole2").hide();
        $("#appendsHTMLOnRole3").hide();
       
    } else if (role_id == 3) {
        getUsers(window.data.area_id, 2);
        $("#appendsHTMLOnRole1").show();
        $("#appendsHTMLOnRole2").show();
        $("#appendsHTMLOnRole3").hide();
     
    } else if (role_id == 4) {
        getUsers(window.data.area_id, 2);
       $("#appendsHTMLOnRole1").show();
        $("#appendsHTMLOnRole2").show();
        $("#appendsHTMLOnRole3").show();
    }
});
 $("select[name=pid2]").change(function () {
            let id = $(this).children('option:selected').val();
            window.data.pid2 = id;
            if(window.data.role_id > 2){
             getUsers(window.data.area_id, 3)
            }else{
            $("#appendsForm").append('<input type="hidden" name="pid" value="'+ id +'" />')
            }
      
}) 

 $("select[name=pid3]").change(function () {
            let id = $(this).children('option:selected').val();
            window.data.pid3 = id;
            if(window.data.role_id > 3){
            getUsers(window.data.area_id, 4)
            }else{
            $("#appendsForm").append('<input type="hidden" name="pid" value="'+ id +'" />')
            }
          
}) 

 $("select[name=pid4]").change(function () {
            let id = $(this).children('option:selected').val();
            window.data.pid4 = id;
            $("#appendsForm").append('<input type="hidden" name="pid" value="'+ id +'" />')
      
}) 

EOF;



        return parent::render(); // TODO: Change the autogenerated stub
    }
}